package algorithm.leetcode;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wuwen
 * @since 2023/12/18
 */
public class NO128_最长连续序列 {

    public static void main(String[] args) {
        System.out.println(new Solution().longestConsecutive(new int[]{100, 4, 200, 1, 3, 2, 3}));
    }

    static class SolutionPerformance {
        public int longestConsecutive(int[] handles) {
            int answer = 0;
            Set<Integer> appearSet = Arrays.stream(handles).boxed().collect(Collectors.toSet());
            for (int handle : handles) {
                // 只有在不是第一个数字的时候才去进行接下来的寻找
                if (!appearSet.contains(handle - 1)) {

                }
            }
            return answer;
        }
    }

    static class SolutionSort {
        public int longestConsecutive(int[] handles) {
            int answer = 0;
            // 去重且排序
            handles = Arrays.stream(handles).sorted().distinct().toArray();
            int size = handles.length, left = 0, right = 0, next;
            while (left < size) {
                ++right;
                next = handles[left] + 1;
                while (right < size && handles[right] == next) {
                    ++right;
                    ++next;
                }
                answer = Math.max(answer, right - left);
                left = right;
            }
            return answer;
        }
    }

    static class Solution {
        public int longestConsecutive(int[] handles) {
            // 存储数字和他的下标
            Set<Integer> mapping = new HashSet<>();
            for (int handle : handles) {
                mapping.add(handle);
            }
            int answer = 0;
            for (int handle : handles) {
                if (!mapping.contains(handle - 1)) {
                    int temp = 1;
                    int next = handle;
                    while (mapping.contains(next + 1)) {
                        ++next;
                        ++temp;
                    }
                    answer = Math.max(answer, temp);
                }
            }
            return answer;
        }
    }
}